package com.peanut.huibutie.openapi;

import com.peanut.huibutie.common.*;
import com.peanut.huibutie.entity.EmployeeInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class GetEmployeeInfo {
    @Resource
    private HttpInvoker httpInvoker;

    @Resource
    private UserContext userContext;
    @Resource
    private AppConfig appConfig;

    public void invoke() {
        Map<String, String> map = new HashMap<>();
        map.put("open_id", userContext.getContext().getOpenId());
        map.put("access_token", userContext.getContext().getAccessToken());
        OpenApiResult<EmployeeInfo> openApiResult = httpInvoker.sendGet(AppConfig.GetEmployeeInfo_URL, map, EmployeeInfo.class);
        if (openApiResult.isSuccess()) {
            EmployeeInfo employeeInfo = openApiResult.getData();
            if (StringUtils.isNotBlank(employeeInfo.getMobile())) {
                employeeInfo.setMobile(AesEncryptUtil.decrypt(employeeInfo.getMobile(), appConfig.getAppSecret()));
            }
            log.info("员工信息={}", employeeInfo);
        }
    }
}
